﻿using System;
using System.IO;
using System.Windows.Forms;

namespace JustMetClient.Tools.FileUtil
{
    /// <summary>
    /// 文件操作工具
    /// </summary>
    public class FileTool
    {
        /// <summary>
        /// 文件复制
        /// </summary>
        /// <param name="oldFilePath"></param> 源文件路径
        /// <param name="fileName"></param> 新文件名称
        /// <param name="newPath"></param> 新文件夹路径
        /// <returns></returns>
        public static bool FileCopy(string oldFilePath, string fileName, string newPath)
        {
            try
            {
                //新路径不存在则创建
                if (Directory.Exists(newPath) == false)
                {
                    //如果不存在，创建
                    Directory.CreateDirectory(newPath);
                }
                //新路径新有重名文件则删除
                string newFullPath = newPath + fileName + Path.GetExtension(oldFilePath);
                if (File.Exists(newFullPath))
                {
                    File.Delete(newFullPath);
                }
                //进行文件复制
                File.Copy(oldFilePath, newFullPath, true);

                return true;
            }
            catch (Exception e)
            {
                Console.WriteLine("Double copy is not allowed, which was not expected.");
            }

            return false;
        }

        #region 选择文件路径
        /// <summary>
        /// 选择文件路径
        /// </summary>
        /// <returns></returns>
        public static string SelectFilePath()
        {
            string path = string.Empty;
            OpenFileDialog fileDialog = new OpenFileDialog();
            DialogResult result = fileDialog.ShowDialog();
            if (result == System.Windows.Forms.DialogResult.OK)
            {
                path = fileDialog.FileName;
            }
            return path;
        }

        #endregion
    }
}
